/**
 * 项目名称: Topic_783.cpp
 * 项目路径: study/DataStructure/LeetCode/树/简单
 * 编辑时间: 2024年08月23日07时51分35秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector<int> vn;
    int ans = 1e6;

    void dfs(TreeNode *root) {
        if (!root)return;

        dfs(root->left);
        vn.push_back(root->val);
        dfs(root->right);
    }

    int minDiffInBST(TreeNode *root) {
        if (!root)return ans;
        dfs(root);
        for (int i = 0; i < vn.size() - 1; i++)
            if (vn[i + 1] - vn[i] < ans && i+1 <=vn.size()-1)
                ans = vn[i + 1] - vn[i];
        return ans;
    }
};
